enyo.kind({
    name: "HPMobileLoginSetup",
	kind: "Component",
    events: {
        onLoginSetupFailure:"",
        onLoginSetupSuccess:""
    },
    components: [
        // HPMobileLogin Service: will be used to retrieve dynamically the definition (source code) of the actual HPMobileLogin kind
        {
            name: "loadLoginKind",
            kind: enyo.PalmService,
            service: "palm://com.hpit.mobile.login.service",
            method: "retrieveSessionInfo",
            onSuccess: "loadLoginKindSuccess",
            onFailure: "loadLoginKindFailure",
            timeout: 30000 //specified in milliseconds
        }
    ],
    setup: function () {
        this.log("Calling Login service to load dynamically definition of HPMobileLogin kind");
        this.$.loadLoginKind.call({prop: "HPMobileLoginKind"});
	},
    //-----------------------------------------------------------------
    // Load Login Kind callbacks
    //-----------------------------------------------------------------
    loadLoginKindSuccess: function(inSender, inResponse){
        this.log("Call to palm://com.hpit.mobile.login.service/retrieveSessionInfo(prop=HPMobileLoginKind) succeeded:", enyo.json.stringify(inResponse));
        
        if(inResponse && inResponse.val){
            var loginKindDefinition=inResponse.val;
            this.log("The definition of HPMobileLoginKind is loaded successfully.",loginKindDefinition);
            var tmpFunc = new Function(loginKindDefinition);
            tmpFunc();
            this.doLoginSetupSuccess();
        }else{
            this.doLoginSetupFailure($L("Unable to set up login. Please make sure application \"HP Mobile Login\" is installed and run at least once."));
            this.error("Unable to load the definition of HPMobileLoginKind. Empty answer received from service call");
        }
    },
    loadLoginKindFailure: function(inSender, inResponse){
        this.error("Call to palm://com.hpit.mobile.login.service/retrieveSessionInfo(prop=HPMobileLoginKind) failed:", enyo.json.stringify(inResponse));
        this.doLoginSetupFailure($L("Unable to set up login. Please make sure application \"HP Mobile Login\" is installed and run at least once."));
    }
});